/**
 * Copyright [2019] [LiBo/Alex of copyright liboware@gmail.com ]
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hyts.monitor.profile.handler;

import com.hyts.monitor.profile.bean.TimeBean;
import com.hyts.monitor.profile.context.ProfilerContext;
import com.hyts.monitor.profile.error.ProfilerException;
import com.hyts.monitor.profile.model.Scope;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;

import java.util.Calendar;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * @project-name:coffice-toolbox
 * @package-name:com.hyts.monitor.profile.thread
 * @author:LiBo/Alex
 * @create-date:2019-08-05 14:44
 * @copyright:libo-alex4java
 * @email:liboware@gmail.com
 * @description:
 */
public class TimeFixExecutionHandler implements Runnable{

    /**
     * 开始时间
     */
    private TimeBean startTime;

    /**
     * 结束时间
     */
    private TimeBean endTime;

    /**
     * 是否允许状态 代替interrupted
     */
    @Getter
    @Setter
    private boolean start;


    /**
     * 操作服务
     * @param startTime
     * @param endTime
     * @param start
     */
    public TimeFixExecutionHandler(String startTime, String endTime, boolean start) {
        this.startTime = TimeBean.of(startTime);
        this.endTime = TimeBean.of(endTime);
        this.start = start;
    }


    @Override
    public void run() {
        //操作服务
        try {
            while(start){
                Calendar calendar = Calendar.getInstance();
                TimeBean now = new TimeBean(calendar.get(Calendar.HOUR_OF_DAY),
                        calendar.get(Calendar.MINUTE),calendar.get(Calendar.SECOND));
                if(now.isLessThan(startTime) || now.isMoreThan(endTime)){
                    ProfilerContext.getProfiler().getAttribute().getWorking().set(false);
                    //TODO 切换滚动方式进行文件滚动
                    ProfilerContext.fileLock = true;
                    TimeUnit.SECONDS.sleep(30);
                }else{
                    //TODO 开始运作服务操作事件控制-进行推出相关的服务和数显通知机制
                }
            }
        } catch (InterruptedException e) {
            throw new ProfilerException("时间处理器操作失败!",e);
        }
    }

}
